package com.remote.server.Asyn;

import com.alibaba.fastjson.JSONObject;
import com.remote.common.BlackList;
import com.remote.server.mapper.BlackMapper;
import com.remote.server.service.BlackService;
import com.smartcar.common.domain.User;
import com.smartcar.common.result.Result;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.Date;
import java.util.List;
import java.util.concurrent.CompletableFuture;

@Component
@Log4j2
public class BlackAsyn {

    @Autowired
    private RedisTemplate<String,String> redisTemplate;

    @Autowired
    private BlackMapper blackMapper;


    /**
     * 自动封号，根据用户的信誉积分来进行封号
     * 信誉积分<=60 就修改账号状态
     */
    @PostConstruct
    public void task() {
        CompletableFuture.supplyAsync(()->{
            List<String> userList = redisTemplate.opsForList().range("user", 0, -1);

//        for (String s : userList) {
//            System.out.println("----------------------------------------------------------");
//            System.out.println("s: "+ s);
//            User user = JSONObject.parseObject(s, User.class);
//            System.out.println("user: " +user);
//        }

            System.out.println("---------------------------------------------------------------------");

            userList.stream()
                    .forEach(user -> {
                        User user1 = JSONObject.parseObject(user, User.class);
                        if (user1.getCreditScore()!=null && user1.getCreditScore() <= 60 && user1.getStatus()!=3) {
                            System.out.println("------------------------------------");
                            Date date = new Date();
                            int month = date.getMonth()+3;
                            int day = date.getDay();
                            int hours = date.getHours();
                            int minutes = date.getMinutes();
                            int seconds = date.getSeconds();
                            int year = date.getYear()+1900;
                            String time="";
                            time= year+"-"+month+"-"+day+" "+hours+":"+minutes+":"+seconds;
                            System.out.println("-----------------------"+time);
                            blackMapper.updateStatus(user1.getPhone(),time);
                            log.info(user1.getPhone() + "该手机号信誉积分过低已被封号");
                        }else {
                            log.info("扫描完毕哦");
                        }
                    });

            log.info("异步线程使用成功");
            return Result.success();
        });


    }



}
